//
//  ALEmoticonToolBar.swift
//  键盘-表情处理
//
//  Created by Miaoyang on 16/4/19.
//  Copyright © 2016年 Miaoyang. All rights reserved.
//

import UIKit

//1.定义代理协议
protocol ALEmoticonToolBarDelegate:NSObjectProtocol{
    //2.定义代理方法
    func emoticonToolBar(toolBar: ALEmoticonToolBar, selectedButtonType type: ALEmoticonToolBarType)
}




//5.实现代理方法

/// 按照类型创建枚举
enum ALEmoticonToolBarType:Int {
    
    case Recent = 0 //最近
    case Default = 1 //默认
    case Emoji = 2 //Emoji
    case Lxh = 3 //浪小花
    
    
}


class ALEmoticonToolBar: UIView {
    
    //3.设置代理属性
    weak var delegate:ALEmoticonToolBarDelegate?
    ///定义一个被点击的按钮记录选中状态
    private var selectorButton:UIButton?
    
    // 加载xib
    class func emoticonToolBar() -> ALEmoticonToolBar {
      
        return NSBundle.mainBundle().loadNibNamed("ALEmoticonTooleBar", owner: nil, options: nil).last as! ALEmoticonToolBar
  }

    @IBAction func didClickButton(sender: UIButton) {
        
        let type = ALEmoticonToolBarType(rawValue:sender.tag)!
        switchSelectedButton(sender)
        //4.调用代理
        delegate?.emoticonToolBar(self, selectedButtonType: type)
    
    }
    //通过collectionView的section切换选中按钮
    func switchSelectButton(section: Int) {
        // 找到对应的按钮
        let button = self.subviews[section] as! UIButton
        

        switchSelectedButton(button)
        
    }
    
    //通过直接点击切换选中按钮
    private func switchSelectedButton(button: UIButton){
        //如果点击的和选中的是同一个按钮就直接返回不做处理事件
        if button == selectorButton{
            
            return
        }
        //1.取消选中的按钮
        selectorButton?.selected = false
        //2.选中当前的按钮
        button.selected = true
        //3.将当前按钮的值赋值给选中按钮
        selectorButton = button
        
    }

}
